package com.guigusuqi.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.app.entity.Registration;
import com.guigusuqi.app.entity.TimeManagement;
import com.guigusuqi.app.service.RegistrationService;
import com.guigusuqi.app.service.TimeManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * <p>
 * 预约挂号管理 前端控制器
 * </p>
 *
 * @author suqi
 * @since 2022-04-05
 */
@RestController
@RequestMapping("/yy/registration")
@Api(tags = {"wx-挂号小程序Api"})
public class RegistrationController
{
    @Resource
    private RegistrationService registrationService;

    @Resource
    private TimeManagementService timeManagementService;

    @ApiOperation(value = "挂号操作")
    @PostMapping("yyregistration")
    public Result yyRegistration(@RequestBody Registration registration)
    {
        QueryWrapper<Registration> wrapperRe = new QueryWrapper<>();
        String timeid = registration.getTimeid();
        String identical = registration.getIdentical();
        if (StringUtils.isEmpty(registration.getStatus()))
        {
            registration.setStatus("0");
        }
        wrapperRe.eq("timeid",timeid).eq("identical",identical);

        int count = registrationService.count(wrapperRe);
        if (count > 0)
        {
            return Result.success().message("请不要重复挂号");
        }
        QueryWrapper<TimeManagement> wrapper = new QueryWrapper<>();
        Integer doctorId = registration.getDoctorId();

        wrapper.eq("doctor_id",doctorId).eq("id",timeid);
        TimeManagement one = timeManagementService.getOne(wrapper);

        if (one!=null && one.getListnum() > 0)
        {
            registration.setPrice(one.getPrice());
            one.setListnum(one.getListnum()-1);
            boolean b = timeManagementService.updateById(one);
            boolean save = registrationService.save(registration);
            if (b && save)
            {
                return Result.success().message("挂号成功，可前去我的挂号查看预约情况");
            }else
            {
                return Result.success().message("挂号失败,系统异常");
            }
        }else
        {
            return Result.success().message("挂号失败，检查余号是否为空");
        }
    }

    @ApiOperation(value = "取消挂号")
    @DeleteMapping("/{id}")
    public Result remove(@ApiParam(name = "id",value = "预约信息id") @PathVariable String id)
    {
        Registration registration = registrationService.getById(id);
        //获取排班信息
        String timeId = registration.getTimeid();
        TimeManagement timeManagement = timeManagementService.getById(timeId);
        //余号+1
        timeManagement.setListnum(timeManagement.getListnum()+1);
        boolean b1 = timeManagementService.updateById(timeManagement);
        boolean b = registrationService.removeById(id);

        if (b && b1)
        {
            return Result.success().message("取消挂号成功");
        }else
        {
            return Result.fail().message("取消挂号失败");
        }
    }

    @ApiOperation(value = "查看我的挂号信息")
    @GetMapping("/{openid}")
    public Result getByOpenId(@ApiParam(name = "openid",value = "openid") @PathVariable String openid)
    {
        QueryWrapper<Registration> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        List<Registration> list = registrationService.list(wrapper);
        return Result.success().data("item",list);
    }

//    /**
//     * 跳转课程首页
//     * @return
//     */
//    @GetMapping("index")
//    public String index()
//    {
//        return "index";
//    }
//
//
//    /**
//     * 查询所有挂号信息
//     * @return
//     */
//    @ResponseBody
//    @GetMapping("/loadRegistration")
//    public Result loadRegistration()
//    {
//        return Result.success().data("registrations", registrationService.list(null));
//    }
//
    /**
     * 跳转课程明细
     * @param id
     * @param modelMap
     * @return
     */
    @GetMapping("registrationDetail/{id}")
    public Result registrationDetail(@PathVariable("id") String id, ModelMap modelMap)
    {
        Registration registration = registrationService.getById(id);
        return Result.success().data("rows",registration);
    }

}

